package com.example.controller;

import com.example.common.Result;
import com.example.domain.Shop;
import com.example.service.ShopService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/shop")
public class ShopController {

    @Resource
    private ShopService shopService;


    /**
     * 根据id查询商铺信息
     *
     * @param id 商铺id
     * @return 商铺详情数据
     */
    @GetMapping("/{id}")
    public Result<Shop> queryShopById(@PathVariable("id") Long id) {

        Result result = shopService.queryById(id);
        //使用redis后
        return result;
    }

    /**
     * 缓存穿透——互斥锁
     *
     * @param id
     * @return
     */
    @GetMapping("/breakdown/{id}")
    public Result<Shop> queryShopById02(@PathVariable("id") Long id) {
        Result result = shopService.queryById02(id);
        if (result == null) {
            return Result.fail("店铺不存在！！");
        }
        //使用redis后
        return result;
    }

    /**
     * 逻辑过期
     *
     * @param id
     * @return
     */
    @GetMapping("/logicExpire/{id}")
    public Result<Shop> queryShopById03(@PathVariable("id") Long id) {

        Result result = shopService.queryById03(id);
        if (result == null) {
            return Result.fail("店铺不存在！！");
        }
        //使用redis后
        return result;
    }

    /**
     * 布隆过滤器解决缓存穿透问题
     *
     * @param id
     * @return
     */
    @GetMapping("/bloomFilter/{id}")
    public Result<Long> queryShopById04(@PathVariable("id") Long id) {

        Shop shop = shopService.bloomFilterById(id);
        if (shop == null) {
            Result.fail("店铺不存在！！");
        }
        //使用redis后
        return Result.ok(shop);
    }

    /**
     * 新增商铺信息
     *
     * @param shop 商铺数据
     * @return 商铺id
     */
    @PostMapping
    public Result saveShop(@RequestBody Shop shop) {
        boolean saveData = shopService.saveData(shop);
        if (!saveData) {
            return Result.fail("新增商铺失败");
        }
        // 返回店铺id
        return Result.ok(shop.getId());
    }

    /**
     * 更新商铺信息
     *
     * @param shop 商铺数据
     * @return 无
     */
    @PutMapping
    public Result updateShop(@RequestBody Shop shop) {
        //写入数据库
        Result update = shopService.update(shop);
        //判断是否更新成功
        if (update == null) {
            return Result.fail("店铺信息更新失败,请检查异常！");
        }
        return Result.ok("店铺信息更新成功");
    }

    /**
     * 根据商铺类型分页查询商铺信息
     *
     * @param typeId  商铺类型
     * @param current 页码
     * @return 商铺列表
     */
    @GetMapping("/of/type")
    public Result queryShopByType(
            //required=false表示参数可以为空，没传参数的的时候正常往下查询
            @RequestParam("typeId") Integer typeId,
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam(value = "x", required = false) Double x,
            @RequestParam(value = "y", required = false) Double y
    ) {
        return shopService.queryShopByType(typeId, current, x, y);
    }

    /**
     * 根据商铺名称关键字分页查询商铺信息
     * @param name 商铺名称关键字
     * @param current 页码
     * @return 商铺列表
     */
    //@GetMapping("/of/name")
    //public Result queryShopByName(
    //        @RequestParam(value = "name", required = false) String name,
    //        @RequestParam(value = "current", defaultValue = "1") Integer current
    //) {
    //    // 根据类型分页查询
    //    Page<Shop> page = shopService.query()
    //            .like(StrUtil.isNotBlank(name), "name", name)
    //            .page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
    //    // 返回数据
    //    return Result.ok(page.getRecords());
    //}
}
